//
//  YTFTabBar.m
//  weibo_yoo_OC
//
//  Created by pro on 16/2/27.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "YTFTabBar.h"

@interface YTFTabBar ()



@end


@implementation YTFTabBar

//自定义一个tabbar
//第一步：重写init,这个方法在init和initWithFrame都会调用
-(instancetype)initWithFrame:(CGRect)frame{
    
    //调用父类的方法
  self = [super initWithFrame:frame];
    
    //调用私有方法来添加视图并设置约束
    [self setupUI];
    
    return self;
}


//第二步：定义一个私有方法，来添加视图并设置约束
-(void)setupUI{
    
    //添加子控件
    [self addSubview:self.composeButton];
    
}



//第三步：懒加载
-(UIButton *)composeButton{
    
    if (_composeButton == nil) {
        
        _composeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        
        //设置composeButton的属性
        [_composeButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [_composeButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        [_composeButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        [_composeButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        
        //设置_composeButton的大小
        [_composeButton sizeToFit];
    
    }
   
    return _composeButton;
    
    
}



////设置子控件的frame
-(void)layoutSubviews{
    //调用父类的方法
    [super layoutSubviews];
    
    //参数
    NSInteger i = 0;
    CGFloat width = self.bounds.size.width / 5;
    CGFloat height = self.bounds.size.height;
    
    for (UIView *item in self.subviews) {
        if ([item isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            
            item.frame = CGRectMake(width * (i++), 0, width, height);
            if (i == 2) {
                
                i++;
                
            }
        }
    }
    
    self.composeButton.frame = CGRectMake(width * 2 , 0, width, height);

    
}



@end
